﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ShoppingCartItemRepository : IShoppingCartItemRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IShoppingCartItemRepository Members

		public int CreateNewShoppingCartItem(Data.Entity.ShoppingCartItem theNewShoppingCartItem)
		{
			if (theNewShoppingCartItem == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ShoppingCartItem_Create(theNewShoppingCartItem.ShoppingCartTypeID, theNewShoppingCartItem.UserID, theNewShoppingCartItem.ProductID,
					theNewShoppingCartItem.AttributesXML, theNewShoppingCartItem.UserEnteredPrice, theNewShoppingCartItem.Quantity, theNewShoppingCartItem.CreatedOn, theNewShoppingCartItem.UpdatedOn);
			}

			return result;
		}

		public int DeleteShoppingCartItem(int id)
		{
			if (id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ShoppingCartItem_Delete(id);
			}

			return result;
		}

		public IList<Data.Entity.ShoppingCartItem> GetShoppingCartItemByUser(System.Guid userID)
		{
			if (userID == null)
			{
				return null;
			}

			List<ECommerce_GetShoppingCartItem_By_UserResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetShoppingCartItem_By_User(userID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ShoppingCartItem>();

				foreach(var sc in lstData)
				{
					var theEntity = new Data.Entity.ShoppingCartItem()
					{
						AttributesXML = sc.AttributesXML,
						CreatedOn = sc.CreatedOn,
						ProductID = sc.ProductID,
						Quantity = sc.Quantity,
						ShoppingCartItemID = sc.ShoppingCartItemID,
						ShoppingCartTypeID = sc.ShoppingCartTypeID,
						UpdatedOn = sc.UpdatedOn,
						UserEnteredPrice = sc.UserEnteredPrice,
						UserID = sc.UserID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.ShoppingCartType> GetAllShoppingCartType()
		{
			List<ECommerce_GetShoppingCartTypeResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetShoppingCartType();

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ShoppingCartType>();

				foreach(var sct in lstData)
				{
					var theEntity = new Data.Entity.ShoppingCartType()
					{
						Name = sct.Name,
						ShoppingCartTypeID = sct.ShoppingCartTypeID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateShoppingCartItem(Data.Entity.ShoppingCartItem theShoppingCartItem)
		{
			if (theShoppingCartItem == null || theShoppingCartItem.ShoppingCartItemID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbShoppingCartItem = (from sci in _dbContext.ShoppingCartItems
										  where sci.ShoppingCartItemID == theShoppingCartItem.ShoppingCartItemID
										  select sci).FirstOrDefault();

				if(dbShoppingCartItem == null)
				{
					result = 0;
				}
				else
				{
					dbShoppingCartItem.AttributesXML = theShoppingCartItem.AttributesXML;
					dbShoppingCartItem.CreatedOn = theShoppingCartItem.CreatedOn;
					dbShoppingCartItem.ProductID = theShoppingCartItem.ProductID;
					dbShoppingCartItem.Quantity = theShoppingCartItem.Quantity;
					dbShoppingCartItem.ShoppingCartTypeID = theShoppingCartItem.ShoppingCartTypeID;
					dbShoppingCartItem.UpdatedOn = theShoppingCartItem.UpdatedOn;
					dbShoppingCartItem.UserEnteredPrice = theShoppingCartItem.UserEnteredPrice;
					dbShoppingCartItem.UserID = theShoppingCartItem.UserID;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;

		}

		public Data.Entity.ShoppingCartItem GetTheShoppingCartItem(int id)
		{
			if(id <= 0)
			{
				return null;
			}

			List<ECommerce_GetTheShoppingCartItemResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheShoppingCartItem(id);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var cartItem = lstData[0];

				Data.Entity.ShoppingCartItem result;

				result = new Data.Entity.ShoppingCartItem()
				{
					AttributesXML = cartItem.AttributesXML,
					CreatedOn = cartItem.CreatedOn,
					ProductID = cartItem.ProductID,
					Quantity = cartItem.Quantity,
					ShoppingCartItemID = cartItem.ShoppingCartItemID,
					ShoppingCartTypeID = cartItem.ShoppingCartTypeID,
					UpdatedOn = cartItem.UpdatedOn,
					UserEnteredPrice = cartItem.UserEnteredPrice,
					UserID = cartItem.UserID
				};

				return result;
			}

			return null;
		}

		#endregion
	}
}
